// pages/products/flash_sale.js
var app = getApp();
function withData(param) {
    return param < 10 ? '0' + param : '' + param;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tapOnSale : true,
      /*products_allData: {
        soonSale:[{
          url: '/images/zfb.png',
          name: '浙江嘉兴鲜美五芳斋粽子特色包装促销中',
          time: 36,
          newprice: '32.88',
          oldprice: '99.00',
          isRemind: true,
          }, {
          url: '/images/zfb.png',
          name: '杭州特色包装促销中',
          time: 33,
          newprice: '32.88',
          oldprice: '99.00',
          isRemind: true,
          }, {
              url: '/images/zfb.png',
              name: '杭州特色包装22222促销中',
              time: 30,
              newprice: '32.88',
              oldprice: '99.00',
              isRemind: true,
          }],
       onSale: [{
            url: '/images/zfb.png',
            name: '浙江嘉兴鲜美五芳斋粽子特色包装促销中',
            time: 16,
            newprice: '32.88',
            oldprice: '99.00',
            isRemind: true,
            progress_active: 30
        }, {
            url: '/images/zfb.png',
            name: '杭州特色包装促销中',
            time: 35,
            newprice: '32.88',
            oldprice: '99.00',
            isRemind: true,
            progress_active: 70
        }, {
            url: '/images/zfb.png',
            name: '杭州特色包装促销中',
            time: 36,
            newprice: '32.88',
            oldprice: '99.00',
            isRemind: true,
            progress_active: 90
        }]}, */
      products_allData: {
          soonSale: [],
          onSale: []
        },
      products:[]
  },
  onLoad(options) {
  },
  onReady() {
        this.tap_onsale();
        setInterval(function(){
            this.countDown();
        }.bind(this),1000)
        var that = this;
        var shopid = wx.getStorageSync('shop_id');
        console.log(shopid)
        app.func.req('ms_products/index', {
            //shop_id: shopid,
            shop_id: 46,
            page_index: 1,
            page_size: 5,
            is_business: 0,
        }, 'GET', function (res) {
            var responseData = res.result;
            console.log(responseData)
            var nowtime = Date.parse(new Date());
            var soonsaleArr = responseData.filter(function(el){
                el.start_time = parseInt(el.start_time) +60*60*26; //测试 加25小时
                el.start_time = el.start_time * 1000;
                return el.start_time>nowtime; //即将开抢
            })
            soonsaleArr.map(function(el){
                var showtime = el.start_time - nowtime;
                el.timestamp = showtime;
                el.time = that.timestamp_to_time(showtime);
                el.end_time = el.end_time*1000;
                el.logo = el.logo.split("!")[0];
                el.isRemind = true;
            })
            var onsaleArr = responseData.filter(function (el) {
                el.start_time = parseInt(el.start_time) + 60 * 60 * 26; //测试 加25小时
                el.start_time = el.start_time * 1000;
                el.end_time = el.end_time * 1000;
                return el.start_time < nowtime && el.end_time>nowtime; //抢购中
            })
            onsaleArr.map(function (el) {
                var showtime = el.end_time - nowtime;
                el.timestamp = showtime;
                el.time = that.timestamp_to_time(showtime);
                el.logo = el.logo.split("!")[0];
            })
            var allproducts = that.data.products_allData;
            allproducts.soonSale = soonsaleArr;
            allproducts.onSale = onsaleArr;
            that.setData({
                products_allData: allproducts
            })    
            console.log(that.data.products_allData) 
        }, {
                'token': wx.getStorageSync('token')
            });
  }, 
  tap_onsale(){
        this.setData({
            tapOnSale: true,
            products: this.data.products_allData.onSale
        })
  },
  tap_soonsale: function () {
      this.setData({
          tapOnSale: false,
          products: this.data.products_allData.soonSale
      })
  },
  remind(e){
      var i = e.currentTarget.dataset.index;
      var _products = this.data.products;
      _products.map(function(el,idx){
            if(idx == i ){
                el.isRemind = !el.isRemind;
            }
      })
      this.setData({
          products: _products
      })
  },
  toCart(){
      console.log("aaaa");
  },
  countDown(){
      var _products = this.data.products;
      var _allproducts = this.data.products_allData;
      var that = this;
      _products.map(function(el){
          if (el.timestamp>=1000)
            el.timestamp -=1000;
           el.time = that.timestamp_to_time(el.timestamp);
      })
      _allproducts.soonSale.map(function(el){
          if (el.timestamp >= 1000)
              el.timestamp -= 1000;
              el.time = that.timestamp_to_time(el.timestamp);
      })
      _allproducts.onSale.map(function (el) {
          if (el.timestamp >= 1000)
              el.timestamp -= 1000;
              el.time =that.timestamp_to_time(el.timestamp);
      })
      this.setData({
          products: _products,
          products_allData : _allproducts
      })
    
  },
  timestamp_to_time(showtime){
      var hour = Math.floor(showtime / (60 * 60 * 1000));
      hour = withData(hour);
      var minute = Math.floor((showtime - hour * (60 * 60 * 1000)) / (60 * 1000));
      minute = withData(minute);
      var second = Math.floor((showtime - minute * (60 * 1000) - hour * (60 * 60 * 1000)) / 1000);
      second = withData(second);
      return  hour + ":" + minute + ":" + second;
  }
})